home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / QFRIP.ZIP / RIPPER.C < prev    next >
Text File  |  1994-01-03  |  6KB  |  219 lines

  1. /******************************************************************\
  2.  
  3.   ripper.c -- tile ripper for preprocessing background tiles
  4.               by Diana Gruber
  5.  
  6.   compile using large memory model
  7.   requires Fastgraph(tm) or Fastgraph/Light(tm) to link
  8.  
  9. \******************************************************************/
  10.  
  11. #include <fastgraf.h>    /* standard include files                */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #ifdef __TURBOC__
  15.    #include <mem.h>
  16. #else
  17.    #include <memory.h>
  18. #endif
  19.  
  20. int rip(char *filename); /* function declarations                 */
  21. void write_level(void);
  22. void display_level(int col);
  23.  
  24. FILE *stream;            /* level output data file                */
  25.  
  26. int tile_index;          /* keep track of current tile            */
  27. int level_index;         /* keep track of level position          */
  28. int ncols;               /* total columns in level map            */
  29.  
  30. #define TILESIZE  256    /* tiles are 16 pixels x 16 pixels       */
  31. #define TILELIMIT 240    /* only 240 unique tiles                 */
  32. #define MAXTILES  1440   /* up to 6 screens of art, 240 tiles each*/
  33.  
  34. /* the ripper_tiles array is used to store the unique tiles as 
  35.    bitmaps RAM in for easy tile comparisons */
  36.  
  37. unsigned char far ripper_tiles[TILELIMIT][TILESIZE];
  38.  
  39. /* the level_map array stores the information needed to rebuild
  40.    the level from tiles */
  41.  
  42. unsigned char level_map[MAXTILES]; 
  43.  
  44. /**************************** main ********************************/
  45.  
  46. void main(int argc, char *argv[])
  47. {
  48.    register int i;
  49.  
  50.    /* check that an input file was specified                      */
  51.    if (argc < 2)
  52.    {
  53.       printf("Command syntax is: RIPPER infile1 <infile2>...\n");
  54.       exit(1);
  55.    }
  56.  
  57.    /* initialize the video mode to Mode X: 320x200x256            */
  58.    fg_setmode(20);
  59.  
  60.    /* initialize some globals                                     */
  61.    tile_index = 0;
  62.    level_index = 0;
  63.    ncols = 0;
  64.  
  65.    /* set all the tiles in the level map to 255                   */
  66.    memset(level_map, 255, MAXTILES);
  67.  
  68.    /* rip all the files specified on the command line             */
  69.    for (i = 1; i <= argc; i++)
  70.      rip(argv[i]); 
  71.  
  72.    /* have a look at page 1 to see what the tiles look like       */
  73.    fg_setvpage(1);
  74.  
  75.    /* write the level data and tiles out to disk                  */
  76.    write_level();
  77.  
  78.    /* check your work -- reconstruct the pictures from the tiles  */
  79.    for (i = 0; i < ncols; i+=20)
  80.       display_level(i);
  81.  
  82.    /* restore the text video mode and exit                        */
  83.    fg_setmode(3);
  84.    fg_reset();
  85.    exit(0);
  86. }
  87.  
  88. /***************************** rip ********************************/
  89.  
  90. int rip(char *filename)
  91. {
  92.    register int i,n;
  93.    unsigned char new_tile[TILESIZE];
  94.    int x,y,x1,y1;
  95.    int status;
  96.  
  97.    /* if you already have a full screen tiles, return an error    */
  98.    if (tile_index >= TILELIMIT) 
  99.       return(-1);
  100.  
  101.    /* display the PCX file on the visual page                     */
  102.    fg_setpage(0);
  103.    fg_setvpage(0);
  104.    fg_move(0,0);
  105.    status = fg_showpcx(filename,0);
  106.  
  107.    /* return an error code if the pcx file is bad or missing      */
  108.    if (status > 0) 
  109.       return (status);
  110.  
  111.    /* loop on the pcx file, starting at upper left corner, moving
  112.       down the columns in sequence                                */
  113.    for (n = 0; n < TILELIMIT; n++)
  114.    { 
  115.       x = (n/12)*16;
  116.       y = (n%12)*16 + 15;
  117.  
  118.       /* get the new tile bitmap                                  */
  119.       fg_move(x,y);
  120.       fg_getimage(new_tile,16,16);
  121.  
  122.       /* compare the new tile to all the ripper tiles             */
  123.       for (i = 0; i < tile_index; i++)
  124.       {
  125.          if (memcmp(new_tile,ripper_tiles[i],TILESIZE) == 0)
  126.          {
  127.             /* a duplicate tile is found, update the level map    */
  128.             level_map[level_index] = (unsigned char)i;
  129.  
  130.             /* black out the duplicate tile                       */
  131.             fg_setcolor(0);
  132.             fg_rect(x,x+15,y-15,y);
  133.             break;
  134.          }
  135.       }
  136.  
  137.       /* no match was found, therefore the tile must be unique    */
  138.       if (level_map[level_index] == 255)
  139.       {
  140.          /* copy the new tile to the hidden page                  */
  141.          x1 = (tile_index%20)*16;
  142.          y1 = (tile_index/20)*16 + 23;
  143.          fg_transfer(x,x+15,y-15,y,x1,y1,0,1);
  144.  
  145.          /* build the level map with the tile index               */
  146.          level_map[level_index] = (unsigned char)tile_index;
  147.  
  148.          /* hold the array in RAM for later comparisons           */
  149.          memcpy(ripper_tiles[tile_index],new_tile,TILESIZE);
  150.  
  151.          /* we can't have more than 240 unique tiles              */
  152.          tile_index++;
  153.          if (tile_index >= TILELIMIT)
  154.             break;
  155.       }
  156.       level_index++;
  157.    }
  158.  
  159.    /* increment the column count                                  */
  160.    ncols += 20;
  161.    return (0);
  162. }
  163.  
  164. /************************* write level ****************************/
  165.  
  166. void write_level(void)
  167. {
  168.    register int i,j;
  169.  
  170.    /* make a pcx file out of the tile page                        */
  171.    fg_setpage(1);
  172.    fg_setvpage(1);
  173.    fg_makepcx(0,319,8,199,"tiles.pcx");
  174.  
  175.    /* open a binary file for the level array                      */
  176.    stream = fopen("ripper.lev","wb");
  177.  
  178.    /* write out all the columns, 12 tiles per column              */
  179.    j = 0;
  180.    for (i = 0; i < ncols; i++)
  181.    {
  182.       fwrite(&level_map[j],sizeof(char),12,stream);
  183.       j+=12;
  184.    }
  185.    fclose(stream);
  186. }
  187.  
  188. /************************ display level ***************************/
  189.  
  190. void display_level(int col)
  191. {
  192.    register int i,n;
  193.    int x,y,x1,y1;
  194.    int tile;
  195.  
  196.    /* set the visual page to page 0 and erase whatever is on it   */
  197.    fg_setpage(0);
  198.    fg_setvpage(0);
  199.    fg_erase();
  200.  
  201.    /* display the tiles starting at the top of the first column   */
  202.    n = 0;
  203.    for (i = 0; i < 240; i++)
  204.    {
  205.       tile = level_map[col*12+n];
  206.       x = (tile%20)*16;
  207.       y = (tile/20)*16 + 23;
  208.  
  209.       x1 = (i/12)*16;
  210.       y1 = (i%12)*16 + 15;
  211.  
  212.       fg_transfer(x,x+15,y-15,y,x1,y1,1,0);
  213.       n++;
  214.    }
  215.  
  216.    /* wait a bit so you can see what you did                      */
  217.    fg_waitfor(20);
  218. }
  219.